<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item label="是否头像上传" prop="isUpload" :rules="[{ required: true, message: '请选择是否头像上传', trigger: 'change' }]">
            <el-radio-group v-model="form.isUpload">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        显示项一
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item label="显示名称" prop="firstLabel" :rules="[{ required: true, message: '请输入显示项名称', trigger: 'blur' }]">
            <el-input v-model="form.firstLabel" clearable placeholder="请输入显示项名称" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item
            label="请选择关联字段"
            prop="firstColumn"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.firstColumn" clearable placeholder="请选择关联字段">
              <el-option
                v-for="obj in jxx"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        显示项二
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item label="显示名称" prop="secondLabel" :rules="[{ required: true, message: '请输入显示项名称', trigger: 'blur' }]">
            <el-input v-model="form.secondLabel" clearable placeholder="请输入显示项名称" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item
            label="请选择关联字段"
            prop="secondColumn"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.secondColumn" clearable placeholder="请选择关联字段">
              <el-option
                v-for="obj in jxx"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        显示项三
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item label="显示名称" prop="thirdLabel" :rules="[{ required: true, message: '请输入显示项名称', trigger: 'blur' }]">
            <el-input v-model="form.thirdLabel" clearable placeholder="请输入显示项名称" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item
            label="请选择关联字段"
            prop="thirdColumn"
            :rules="[{ required: true, message: '请选择关联字段', trigger: 'change' }]"
          >
            <el-select v-model="form.thirdColumn" clearable placeholder="请选择关联字段">
              <el-option
                v-for="obj in jxx"
                :key="obj"
                :label="obj"
                :value="obj"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        扩展项
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button>
        </div>
      </div>
      <draggable v-model="form.data" animation="300" @end="clone">
        <el-col v-for="(item,index) in form.data" :key="'data-'+index" style="border-bottom: 1px solid #e2e2e2;margin-bottom: 10px;" :span="24">
          <el-row :gutter="10" style="margin-top: 10px">
            <el-col :span="12">
              <el-form-item
                label="显示标题"
                :prop="'data.'+index+'.label'"
                :rules="[{ required: true, message: '请输入显示标题', trigger: 'blur' }]"
                style="width: 100%"
              >
                <el-input v-model="item.label" clearable placeholder="请输入显示项名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="显示图标"
                :prop="'data.'+index+'.icon'"
                :rules="[{ required: true, message: '请选择显示图标', trigger: 'change' }]"
              >
                <el-popover
                  placement="bottom-start"
                  width="450"
                  trigger="click"
                >
                  <el-button slot="reference" size="small" style="padding: 0">
                    <el-image
                      v-if="item.icon"
                      style="height: 32px;width: 32px;"
                      :src="item.icon"
                    />
                    <i v-else style="font-size: 20px;height: 32px;width: 32px;line-height: 32px" class="el-icon-plus" />
                  </el-button>
                  <abbreviation-image-select ref="iconSelect" v-model="item.icon" />
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="数据来源"
                :prop="'data.'+index+'.sjly'"
                style="width: 100%"
              >
                <el-popover
                  placement="bottom-start"
                  width="450"
                  trigger="click"
                >
                  <notice-data ref="tempSelect" v-model="item.sjly" />
                  <el-input slot="reference" v-model="item.sjly.name" style="width: 450px" placeholder="点击选择模版" readonly />
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col v-if="item.sjly.jxx" :span="24">
              <el-form-item
                label="数据项"
                :prop="'data.'+index+'.number'"
                :rules="[{ required: true, message: '请选择数据项', trigger: 'change' }]"
              >
                <el-select v-model="item.number" clearable placeholder="请选择数据项" style="width: 450px">
                  <el-option
                    v-for="obj in item.sjly.jxx"
                    :key="obj"
                    :label="obj"
                    :value="obj"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否打开详情" :prop="'data.'+index+'.isOpenMore'" :rules="[{ required: true, message: '请选择是否打开详情', trigger: 'change' }]">
                <el-radio-group v-model="item.isOpenMore">
                  <el-radio
                    v-for="item in status"
                    :key="item.id"
                    :label="item.value"
                  >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col v-if="item.isOpenMore === true " :span="12">
              <el-form-item label="打开方式" :prop="'data.'+index+'.openType'" :rules="[{ required: true, message: '请选择打开方式', trigger: 'change' }]">
                <el-radio-group v-model="item.openType">
                  <el-radio
                    v-for="item in openType"
                    :key="item.id"
                    :label="item.value"
                  >{{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col v-if="item.isOpenMore === true && item.openType === 1" :span="12">
              <el-form-item
                label="关联模版"
                :prop="'data.'+index+'.temp'"
                :rules="[{ required: true, message: '请选择关联模版', trigger: 'change' }]"
              >
                <el-select v-model="item.temp" clearable filterable placeholder="请选择关联模版">
                  <el-option
                    v-for="obj in tempList"
                    :key="obj.id"
                    :label="obj.name"
                    :value="obj.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="item.isOpenMore === true && item.openType === 2" :span="12">
              <el-form-item
                label="打开地址"
                :prop="'data.'+index+'.openUrl'"
                :rules="[{ required: true, message: '请输入打开地址', trigger: 'blur' }]"
                style="width: 100%"
              >
                <el-input v-model="item.openUrl" clearable placeholder="请输入打开地址" />
              </el-form-item>
            </el-col>
            <el-col :span="4" style="text-align: center">
              <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(item)">删除</el-button>
            </el-col>
          </el-row>
        </el-col>
      </draggable>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>

<script>
import { queryAllByFl, updateObject } from '@/api/portal/manager/columnTemp'
import { queryAllDataBySystem, queryAllDataSource } from '@/api/portal/manager/analysis'
import NoticeData from '@/components/NoticeData'
import draggable from 'vuedraggable'
import AbbreviationImageSelect from '@/components/AbbreviationImageSelect/index.vue'
export default {
  name: 'PcVisitingCardV1',
  components: { AbbreviationImageSelect, draggable, NoticeData },
  props: {
    data: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      loading: false,
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      openType: [
        { id: 0, value: 1, label: '指定模版' },
        { id: 1, value: 2, label: '固定地址' }
      ],
      jxx: [],
      tempList: [],
      form: {
        isUpload: false,
        firstLabel: '',
        firstColumn: '',
        secondLabel: '',
        secondColumn: '',
        thirdColumn: '',
        thirdLabel: '',
        data: []
      }
    }
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (newName.sjlx === 1) {
            this.initDataJxx()
          } else if (newName.sjlx === 2) {
            this.initSystemJxx()
          }
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx)
            if (this.form.data === undefined) {
              this.form.data = []
            }
            if (this.form.shortColumn === undefined) {
              this.form.shortColumn = []
            }
            if (this.form.query === undefined) {
              this.form.query = []
            }
            if (this.form.entityParams === undefined) {
              this.form.entityParams = []
            }
            if (this.form.params === undefined || this.form.params === null) {
              this.form.params = []
            }
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    // 解决火狐拖拽搜索的问题
    document.body.ondrop = function(event) {
      event.preventDefault()
      event.stopPropagation()
    }
    this.initTemp()
  },
  methods: {
    doAdd() {
      const item = { label: '', sjly: {}, px: 0, number: null, isOpenMore: false, openType: 1, openUrl: '', temp: '' }
      this.form.data.push(item)
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item)
      if (index !== -1) {
        this.form.data.splice(index, 1)
      }
    },
    clone() {
      this.form.data = this.form.data.map((item, index) => {
        item.px = index + 1
        return item
      })
    },
    initDataJxx() {
      queryAllDataSource({ id: this.data.sjx }).then(res => {
        const { data } = res
        this.jxx = data
      })
    },
    initSystemJxx() {
      queryAllDataBySystem({ id: this.data.sjx }).then(res => {
        const { data } = res
        this.jxx = data
      })
    },
    initTemp() {
      queryAllByFl({ mbdl: 'TabsTemplate' }).then(res => {
        const { data } = res
        this.tempList = data.data
      }).catch(() => {
        this.tempList = []
      })
    },
    save() {
      this.loading = true
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form)
          updateObject(this.data).then(res => {
            this.close()
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.loading = false
        }
      })
    },
    selected(name) {
      this.form.appIcon = name.url
    },
    close() {
      this.loading = false
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>
.setting-body {
  width: 100%;
}

.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}

.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
